package com.cuitrip.business.pay.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.business.order.model.OrderItem;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.component.AutoMatchListView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.Utils;
import com.umeng.onlineconfig.OnlineConfigAgent;

/**
 * Created by sheena on 16/2/18.
 */
public class PayMethodAdapter extends BaseAdapter {
    public static final String ADYEN_OPEN = "1";

    private int[] title;
    private int[] paymethodIcon;
    private OrderItem orderItem;
    private String adyenValue ;
    private Context mContext;
    private AutoMatchListView payMethodListView;

    LayoutInflater inflater;

    public PayMethodAdapter(Context context,OrderItem orderItem,AutoMatchListView payMethodListView) {
        mContext=context;
        this.orderItem = orderItem;
        this.payMethodListView=payMethodListView;
        showPayMethod();
    }

    private void showPayMethod() {
        adyenValue = OnlineConfigAgent.getInstance().getConfigParams(mContext, "adyen_pay");
        if ("CNY".equals(orderItem.getPayCurrency().toUpperCase())) {
            //人民币支付
            //微信安装
            if (Utils.isWXInstalled()) {
                if (ADYEN_OPEN.equals(adyenValue)) {
                    title = new int[]{R.string.account_alipay, R.string.account_wechatpay, R.string.account_visa, R.string.account_mastercard};
                    paymethodIcon = new int[]{R.drawable.alipay, R.drawable.wechat, R.drawable.visa, R.drawable.mastercard};
                } else {
                    title = new int[]{R.string.account_alipay, R.string.account_wechatpay};
                    paymethodIcon = new int[]{R.drawable.alipay, R.drawable.wechat};
                }
            } else {
                //微信未安装
                if (ADYEN_OPEN.equals(adyenValue)) {
                    title = new int[]{R.string.account_alipay, R.string.account_visa, R.string.account_mastercard};
                    paymethodIcon = new int[]{R.drawable.alipay, R.drawable.visa, R.drawable.mastercard};
                } else {
                    title = new int[]{R.string.account_alipay};
                    paymethodIcon = new int[]{R.drawable.alipay};
                }
            }

        } else {
            //外币支付,不支持ping++,只显示adyen支付
            if (ADYEN_OPEN.equals(adyenValue)) {
                title = new int[]{R.string.account_visa, R.string.account_mastercard};
                paymethodIcon = new int[]{R.drawable.visa, R.drawable.mastercard};
            }
        }
    }

    @Override
    public int getCount() {
        return title.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.item_pay_method, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvIcon = (IconTextView) convertView.findViewById(R.id.tvIcon);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.checkbox = (IconTextView) convertView.findViewById(R.id.checkbox);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvIcon.setVisibility(View.GONE);
        viewHolder.ivIcon.setImageResource(paymethodIcon[position]);
        viewHolder.title.setText(title[position]);
        viewHolder.checkbox.setText(payMethodListView.isItemChecked(position) ? ResourceUtil.getString(R.string.correct_icon) : ResourceUtil.getString(R.string.unselected_icon));
        viewHolder.checkbox.setTextColor(payMethodListView.isItemChecked(position) ? ResourceUtil.getColor(R.color.apple_7ecc1e) : ResourceUtil.getColor(R.color.ganshi_ded8d7));
        return convertView;
    }

    public class ViewHolder {
        ImageView ivIcon;
        IconTextView tvIcon;
        TextView title;
        IconTextView checkbox;
    }


}
